package com.syni.mdd.sjb.common.component.utils.common.utils;

import com.syni.mdd.sjb.common.component.utils.common.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

/**
 * @author wangbaitao
 * @version 1.0.0
 * @Date 2021/02/25
 * <h>默认的切面异常父类</h>
 */
@ControllerAdvice
@ResponseBody
@Slf4j
public class DefaultExceptionAdvice {
    /**
     * BusinessException 业务异常处理
     */
    @ExceptionHandler(BusinessException.class)
    public Map<String, Object> handleException(BusinessException e) {
        return defHandler(e.getMessage(), e);
    }

    /**
     * 异常日志记录重定向
     *
     * @param msg 错误提示信息
     * @param e   异常对象
     * @return 结果
     */
    private Map<String, Object> defHandler(String msg, Exception e) {
        log.error(msg, e);
        return ResultMapHelper.failure(msg);
    }
}
